﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UI.teacher
{
    public partial class BidTask : Form
    {
        public int T_Id=0;
        public XueMeng.Model.Task ts;
        public XueMeng.Model.Auction au = null;
        public int LoginId = 0;
        public BidTask()
        {
            InitializeComponent();
        }

        private void BidTask_Load(object sender, EventArgs e)
        {
            this.LoginId = Help.UserInfo.User.E_Id;
            if (this.LoginId == 0)
            {
                MessageBox.Show("你还没有登录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            GetData();
            LoadEvent();
        }

        private void GetData()
        {
            if (T_Id == 0)
            {
                MessageBox.Show("获取任务失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            ts = new XueMeng.BLL.TaskManager().GetModel(T_Id); //获取任务
            XueMeng.Model.Student smp = new XueMeng.BLL.StudentManager().GetModel(ts.S_Id); //获取学生信息
            //XueMeng.Model.Auction auc = new XueMeng.BLL.AuctionManager().GetModelList("T_Id=" + ts.T_Id + "")[0]; //获取老师竞拍的信息
            DataTable dt = new XueMeng.BLL.ProblemManager().GetList("T_Id = " + ts.T_Id + "").Tables[0];
            List<XueMeng.Model.Problem> pr = new XueMeng.BLL.ProblemManager().DataTableToList(dt); //获取问题

            DataTable Au_Dt = new XueMeng.BLL.AuctionManager().GetList("T_Id = " + ts.T_Id + " and E_Id=" + this.LoginId + "").Tables[0];
            if (Au_Dt.Rows.Count > 0)
            {
                au = new XueMeng.BLL.AuctionManager().DataTableToList(Au_Dt)[0];
            }

            this.label2.Text = smp.S_Name;
            this.label4.Text = ts.Fraction_Sta.ToString() + "-" + ts.Fraction_Sto.ToString();
            this.label8.Text = "发布时间："+ts.T_Date.ToString();

            this.numericUpDown1.Minimum = ts.Fraction_Sta;
            this.numericUpDown1.Maximum = ts.Fraction_Sto;

            int QuestionNum = pr.Count;
            InitAddTabPages(QuestionNum);
            for (int i = 0; i < QuestionNum;i++ )
            {
                InitAddTabPageControls(i, pr[i].P_Context);
            }
            if (au!=null)
            {
                this.textBox1.Text = au.Solution;
                this.textBox2.Text = au.FinishDate.ToString();
                this.numericUpDown1.Minimum = au.Fraction;
            }
        }

        //动态在tabpage中添加窗体
        //int n 为创建tabpage的个数
        private void InitAddTabPageControls(int n,string text)
        {
            TextBox tb = new TextBox();//动态创建一个窗体
            tb.Dock = DockStyle.Fill;//控件边缘控制
            tb.Multiline = true;
            tb.ScrollBars = ScrollBars.Vertical;
            tb.Enabled = false;
            tb.Text = text;
            this.tabControl1.TabPages[n].Controls.Add(tb);
            //tb.Show();
        }

        //动态添加TabControl中的页（tabpage）项
        //int n 为创建tabpage的个数
        private void InitAddTabPages(int n)
        {
            for (int i = 0; i < n; i++)
            {
                TabPage tabPage = new TabPage();
                tabPage.Text = "问题"+(i+1)+"";
                this.tabControl1.Controls.Add(tabPage);
            }
        }

        private void LoadEvent()
        {
            this.button1.Click += new EventHandler(button1_Click);
            this.button2.Click += new EventHandler(button2_Click);
        }

        void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void button1_Click(object sender, EventArgs e)
        {
            string FinishDate = this.textBox2.Text.ToString(); //完成时间
            string Fraction = this.numericUpDown1.Value.ToString(); //所需分值
            string Solution = this.textBox1.Text; //解决方案
            if (Fraction.Equals("") || Fraction.Equals("0"))
            {
                MessageBox.Show("竞拍分值不正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (Solution.Equals(""))
            {
                MessageBox.Show("解决方案不能够为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.textBox1.Focus();
                return;
            }
            if (XueMeng.Common.Function.RegexNumber(FinishDate) != true)
            {
                MessageBox.Show("完成时间必须为正数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (Convert.ToInt32(Fraction) < ts.Fraction_Sta || Convert.ToInt32(Fraction) > ts.Fraction_Sto)
            {
                MessageBox.Show("竞拍分值超出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (au == null) //添加
            {
                XueMeng.Model.Auction model = new XueMeng.Model.Auction()
                {
                    T_Id = this.T_Id,
                    E_Id = this.LoginId,
                    Fraction = Convert.ToInt32(Fraction),
                    CreateDate = DateTime.Now,
                    FinishDate = Convert.ToInt32(FinishDate),
                    Solution = Solution,
                    Enable = 0
                };
                if (new XueMeng.BLL.AuctionManager().Add(model) == 0)
                {
                    MessageBox.Show("竞拍失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("竞拍成功!", "提示");
                    ((TaskAuction)this.Owner).loadForm();
                    this.Close();
                }
            }
            else //修改
            {
                XueMeng.Model.Auction model = new XueMeng.Model.Auction()
                {
                    A_Id = au.A_Id,
                    Fraction = Convert.ToInt32(Fraction),
                    FinishDate = Convert.ToInt32(FinishDate),
                    Solution = Solution,
                };
                if (new XueMeng.BLL.AuctionManager().Update(model) == true)
                {
                    MessageBox.Show("更新成功!", "提示");
                    ((TaskAuction)this.Owner).loadForm();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("更新失败!", "提示",MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

    }
}
